<?php
/**
 * Created by PhpStorm.
 * User: Wu Ning
 * Date: 2018/5/20
 * Time: 16:20
 */
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Model\Backstage\CustomersFollow;
use App\Model\Backstage\RoomFollow;
use App\Model\Backstage\CustomerFollowSourde;
use App\Model\Backstage\RoomInfo;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Model\Account\Type;

class   RoomFollowController extends Controller
{
    /*
     * 添加房源跟进
     * Wu Ning
     */
    public function addRoomFollow(Request $request,RoomFollow $RoomFollow,CustomerFollowSourde $customerfollowsourde, RoomInfo $roomInfo)
    {
        $params = $request->all();
        $validator = Validator::make($params, [
            'f_user_id'         =>'required',
            'f_department_id'   =>'required',
            'f_room_id'         => 'required',
            'f_follow_up_way'   => 'required',
            'f_content'         => 'required',
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        //开启事务
        DB::beginTransaction();
            $c_id = true;
            $params['f_customers_id'] = 0;

            if ($request->input('f_customers_id')) {
                $params['f_customers_id'] = $request->input('f_customers_id');
                $customerd = new CustomersFollow();
                $par = $params;
                $par['cf_c_id'] = $params['f_customers_id'];
                $par['cf_fllow_type'] = $params['f_follow_up_way'];
                $par['cf_room_id'] = $params['f_room_id'];
                $par['cf_content'] = $params['f_content'];
                $par['cf_user_id'] = $params['f_user_id'];
                $par['cf_department_id'] = $params['f_department_id'];


                $c_id = $customerd->addCustomersFollow($par);
            }
            $r_id = $RoomFollow->addRoomFollow($params,$params['f_customers_id']);

        //添加提醒跟进范围  提醒时间有的话   添加提醒范围
        $cfsid = true;
        if ($request->input('cfs_remind_time')){
            $cfsid = $customerfollowsourde->addCustomerFollowSourde($params,$r_id);
        }

        //更新房源最后跟进时间
        $saveTime = $roomInfo->updateFollowTime($params['f_room_id']);
        if($c_id && $r_id && $cfsid && $saveTime){
            DB::commit();
            return $this->success('ok', '添加成功');
        } else {
            DB::rollback();
            return $this->error(Error::ADD_ERROR);
        }
    }

    /**
     * 修改房源跟进
     */
        public function updateRoomFollow(Request $request,RoomFollow $RoomFollow, RoomInfo $roomInfo){
            $params = $request->all();
            $validator = Validator::make($params, [
               'f_id'         => 'required',
                'f_user_id'         =>'required',
                'f_department_id'   =>'required',
                'f_follow_up_way'   => 'required',
                'f_content'         => 'required',
            ]);
            if ($validator->fails()) {
                return $this->error(Error::PARAM_MISSING);
            }
            $RoomId = $RoomFollow->updateFollow($params);
            if($RoomId){
                return $this->success('ok','修改成功');
            }
            return $this->error(Error:: EDIT_ERROR);
        }

    /**
     * 删除房源跟进
     */
        public function delRoomFollow(Request $request,RoomFollow $RoomFollow){
            if($followId = $request->input('f_id')){
                $rel = $RoomFollow->delFollow($followId);
                if($rel){
                    return $this->success('ok', '删除成功');
                }else{
                    return $this->error(Error::DEL_ERROR);
                }
            }
            return $this->error(Error:: PARAM_MISSING);

        }

    /**
     * 获取房源跟进
     */
        public function getRoomFollow(Request $request,RoomFollow $RoomFollow){
            if($roomId = $request->input('f_room_id')){
                $list = $RoomFollow->roomFollow($roomId, env('PAGE_LIMIT_ADMIN'), 0);
                return $this->success('ok', $list);
            }

            return $this->error(Error:: PARAM_MISSING);

        }
}